package com.yaosheng.thread.lock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * Created by yaosheng on 2021/7/1.
 */
public class TestLockSupport {

    public static void main(String[] args) {

        Thread t = new Thread ( () -> {
            for(int i = 0;i < 10;i ++){
                System.out.println (i);
                if(i == 5){
                    LockSupport.park ();   //使当前线程进入阻塞状态
                }
                try {
                    TimeUnit.SECONDS.sleep (1);   //每隔一秒钟打印一次
                } catch (InterruptedException e) {
                    e.printStackTrace ();
                }
            }
        });

        t.start ();

        try{
            TimeUnit.SECONDS.sleep (8);
        } catch (InterruptedException e) {
            e.printStackTrace ();
        }

        System.out.println ("After 8 seconds");
        LockSupport.unpark (t);      //唤醒阻塞线程
    }
}
